import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 采购组织
 * @date: 2018-7-2


 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { fetchPurchaseOrgList as _fetchPurchaseOrgList, savePurchaseOrg as _savePurchaseOrg } from "../services/purchaseOrgService";
export default {
  namespace: 'purchaseOrg',
  state: {
    purchaseOrgList: [],
    // 采购组织列表数据
    pagination: {} // 分页对象

  },
  effects: {
    // 获取采购组织数据
    fetchPurchaseOrgList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchPurchaseOrgList(_ref, _ref2) {
      var payload, put, call, res, list;
      return _regeneratorRuntime.wrap(function fetchPurchaseOrgList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              _context.next = 4;
              return call(_fetchPurchaseOrgList, parseParameters(payload));

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  purchaseOrgList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
              return _context.abrupt("return", list);

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchPurchaseOrgList);
    }),
    // 批量新增编辑采购组织
    savePurchaseOrg: /*#__PURE__*/_regeneratorRuntime.mark(function savePurchaseOrg(_ref3, _ref4) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function savePurchaseOrg$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_savePurchaseOrg, payload);

            case 4:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, savePurchaseOrg);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref5) {
      var payload = _ref5.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};